#!/usr/bin/env python3
"""
统一的过滤器配置系统
在这里定义所有过滤器，系统会自动生成相关代码
"""

from enum import Enum
from typing import Dict, List, Any, Optional

class FilterType(Enum):
    """过滤器类型"""
    MULTI_SELECT = "multi_select"  # 多选下拉框
    SINGLE_SELECT = "single_select"  # 单选下拉框
    RANGE = "range"  # 范围选择
    NUMBER = "number"  # 单个数字
    TEXT = "text"  # 文本输入
    BOOLEAN = "boolean"  # 布尔选择

class FilterConfig:
    """过滤器配置类"""
    
    def __init__(self, 
                 key: str,
                 label: str,
                 filter_type: FilterType,
                 db_field: str,
                 options: Optional[List[str]] = None,
                 default_value: Any = None,
                 case_sensitive: bool = False,
                 description: str = "",
                 min_value: Optional[float] = None,
                 max_value: Optional[float] = None,
                 step: float = 1.0,
                 placeholder: str = ""):
        self.key = key
        self.label = label
        self.filter_type = filter_type
        self.db_field = db_field
        self.options = options or []
        self.default_value = default_value
        self.case_sensitive = case_sensitive
        self.description = description
        # 数字范围相关
        self.min_value = min_value
        self.max_value = max_value
        self.step = step
        # 文本输入相关
        self.placeholder = placeholder

# 🎯 核心配置：在这里定义所有过滤器
FILTER_CONFIGS = [
    FilterConfig(
        key="symbols",
        label="品种",
        filter_type=FilterType.MULTI_SELECT,
        db_field="symbol",
        options=["MESM25", "ESM25", "MNQM25", "NQMM25", "RTYM25"],
        description="选择交易品种"
    ),
    
    FilterConfig(
        key="directions",
        label="方向",
        filter_type=FilterType.MULTI_SELECT,
        db_field="direction",
        options=["Long", "Short"],
        description="选择交易方向"
    ),
    
    FilterConfig(
        key="trade_types",
        label="类型",
        filter_type=FilterType.MULTI_SELECT,
        db_field="trade_type",
        options=["target", "scalp", "swing", "breakout", "mr", "other"],
        description="选择交易类型"
    ),
    
    FilterConfig(
        key="sides",
        label="Side",
        filter_type=FilterType.MULTI_SELECT,
        db_field="side",
        options=["Left", "Right"],
        description="选择交易Side"
    ),
    
    FilterConfig(
        key="sessions",
        label="时段",
        filter_type=FilterType.MULTI_SELECT,
        db_field="session",
        options=["RTH", "ETH", "GLOBEX"],
        description="选择交易时段"
    ),
    
    FilterConfig(
        key="profit_status",
        label="盈亏状态",
        filter_type=FilterType.MULTI_SELECT,
        db_field="net_profit",  # 特殊处理
        options=["profitable", "losing"],
        description="选择盈亏状态"
    ),
    
    FilterConfig(
        key="nth_trade",
        label="当日第几笔",
        filter_type=FilterType.RANGE,
        db_field="nth_trade",
        min_value=1.0,
        max_value=50.0,
        step=1.0,
        description=""
    ),
    
    # 🆕 未来可以轻松添加新的过滤器
    # FilterConfig(
    #     key="entry_reasons",
    #     label="入场原因",
    #     filter_type=FilterType.MULTI_SELECT,
    #     db_field="entry_reason",
    #     options=["breakout", "pullback", "pattern_completion", "momentum"],
    #     description="选择入场原因"
    # ),

    FilterConfig(
        key="nth_trade",
        label="当天第几笔",
        filter_type=FilterType.NUMBER,
        db_field="nth_trade",
        min_value=1.0,
        max_value=50.0,
        step=1.0,
        description="当天第几笔"
    ),
]

# 生成便于访问的字典
FILTERS_BY_KEY = {config.key: config for config in FILTER_CONFIGS}

def get_filter_config(key: str) -> Optional[FilterConfig]:
    """获取指定过滤器的配置"""
    return FILTERS_BY_KEY.get(key)

def get_all_filter_keys() -> List[str]:
    """获取所有过滤器的key"""
    return list(FILTERS_BY_KEY.keys())

def get_multi_select_filters() -> List[FilterConfig]:
    """获取所有多选过滤器"""
    return [config for config in FILTER_CONFIGS if config.filter_type == FilterType.MULTI_SELECT]

def generate_frontend_config() -> Dict[str, Any]:
    """生成前端配置"""
    return {
        "filters": [
            {
                "key": config.key,
                "label": config.label,
                "type": config.filter_type.value,
                "options": config.options,
                "defaultValue": config.default_value,
                "description": config.description
            }
            for config in FILTER_CONFIGS
        ]
    }

def generate_mock_test_fields() -> List[str]:
    """生成Mock测试需要的字段列表"""
    return [config.db_field for config in FILTER_CONFIGS if config.db_field != "net_profit"] 